package com.lxmes.sales.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import com.lxmes.common.utils.SecurityUtils;
import com.lxmes.sales.domain.ZcQuotationDetails;
import com.lxmes.sales.domain.ZcQuotationOverview;
import com.lxmes.sales.mapper.ZcQuotationDetailsMapper;
import com.lxmes.sales.mapper.ZcQuotationOverviewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lxmes.common.utils.StringUtils;
import com.lxmes.common.utils.bean.BeanValidators;
import com.lxmes.common.exception.ServiceException;
import javax.validation.Validator;
import com.lxmes.sales.mapper.ZcSupplierQuotationMapper;
import com.lxmes.sales.domain.ZcSupplierQuotation;
import com.lxmes.sales.service.IZcSupplierQuotationService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 供方报价Service业务层处理
 *
 * @author lx
 * @date 2023-06-21
 */
@Service
public class ZcSupplierQuotationServiceImpl implements IZcSupplierQuotationService
{
    @Autowired
    private ZcSupplierQuotationMapper zcSupplierQuotationMapper;

    @Autowired
    private ZcQuotationDetailsMapper zcQuotationDetailsMapper;

    @Autowired
    private ZcQuotationOverviewMapper zcQuotationOverviewMapper;

    @Autowired
    protected Validator validator;
    /**
     * 查询供方报价
     *
     * @param id 供方报价主键
     * @return 供方报价
     */
    @Override
    public ZcSupplierQuotation selectZcSupplierQuotationById(Long id)
    {
        return zcSupplierQuotationMapper.selectZcSupplierQuotationById(id);
    }

    /**
     * 查询供方报价列表
     *
     * @param zcSupplierQuotation 供方报价
     * @return 供方报价
     */
    @Override
    public List<ZcSupplierQuotation> selectZcSupplierQuotationList(ZcSupplierQuotation zcSupplierQuotation)
    {
        String username = SecurityUtils.getUsername();
        zcSupplierQuotation.setQuotePerson(username);
        return zcSupplierQuotationMapper.selectZcSupplierQuotationList(zcSupplierQuotation);
    }

    /**
     * 新增供方报价
     *
     * @param zcSupplierQuotation 供方报价
     * @return 结果
     */
    @Override
    public int insertZcSupplierQuotation(ZcSupplierQuotation zcSupplierQuotation)
    {
        zcSupplierQuotation.setQuoteNumber(zcSupplierQuotation.getOrderNumber()+ NumberUtil.decimalFormat("000" , RandomUtil.randomInt(1, 1000)));
        zcSupplierQuotation.setQuoteDate(new Date());
        zcSupplierQuotation.setQuotePerson(SecurityUtils.getUsername());
        zcSupplierQuotation.setDeptId(SecurityUtils.getDeptId());

        ZcQuotationDetails zcQuotationDetails = Convert.convert(ZcQuotationDetails.class, zcSupplierQuotation);
        zcQuotationDetails.setId(null);
        zcQuotationDetails.setConfirmationDate(new Date());
        zcQuotationDetails.setQuotePerson(SecurityUtils.getUsername());
        zcQuotationDetails.setCreateBy(SecurityUtils.getUsername());
        zcQuotationDetails.setCreateTime(new Date());
        zcQuotationDetails.setDeptId(SecurityUtils.getDeptId());
        zcQuotationDetailsMapper.insertZcQuotationDetails(zcQuotationDetails);


        return zcSupplierQuotationMapper.insertZcSupplierQuotation(zcSupplierQuotation);
    }

    /**
     * 修改供方报价
     *
     * @param zcSupplierQuotation 供方报价
     * @return 结果
     */
    @Override
    public int updateZcSupplierQuotation(ZcSupplierQuotation zcSupplierQuotation)
    {
        return zcSupplierQuotationMapper.updateZcSupplierQuotation(zcSupplierQuotation);
    }

    /**
     * 批量删除供方报价
     *
     * @param ids 需要删除的供方报价主键
     * @return 结果
     */
    @Override
    public int deleteZcSupplierQuotationByIds(Long[] ids)
    {
        return zcSupplierQuotationMapper.deleteZcSupplierQuotationByIds(ids);
    }

    /**
     * 删除供方报价信息
     *
     * @param id 供方报价主键
     * @return 结果
     */
    @Override
    public int deleteZcSupplierQuotationById(Long id)
    {
        return zcSupplierQuotationMapper.deleteZcSupplierQuotationById(id);
    }
    @Override
    public String imporIteminfo(List<ZcSupplierQuotation> zcSupplierQuotationList, boolean updateSupport, String operName) {
        if (StringUtils.isNull(zcSupplierQuotationList) || zcSupplierQuotationList.size() == 0)
        {
            throw new ServiceException("导入数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (ZcSupplierQuotation zcSupplierQuotation : zcSupplierQuotationList)
        {
            try
            {
                BeanValidators.validateWithException(validator, zcSupplierQuotation);
                zcSupplierQuotation.setCreateBy(operName);
                zcSupplierQuotation.setUpdateBy(operName);
                this.insertZcSupplierQuotation(zcSupplierQuotation);
                successNum++;
                successMsg.append("<br/>" + successNum + "数据导入成功");
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/> 数据导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int confirm(ZcSupplierQuotation zcSupplierQuotation) {
        zcQuotationDetailsMapper.deleteZcQuotationDetailsById(zcSupplierQuotation.getId());
        ZcQuotationDetails zcQuotationDetails = Convert.convert(ZcQuotationDetails.class, zcSupplierQuotation);
        zcQuotationDetails.setId(null);
        zcQuotationDetails.setConfirmationDate(new Date());
        zcQuotationDetails.setQuotePerson(SecurityUtils.getUsername());
        zcQuotationDetails.setCreateBy(SecurityUtils.getUsername());
        zcQuotationDetails.setCreateTime(new Date());
        zcQuotationDetails.setDeptId(SecurityUtils.getDeptId());
        return zcQuotationDetailsMapper.insertZcQuotationDetails(zcQuotationDetails);
    }
}
